﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.DTOs;

namespace MS.InformationManagement.Core.Support
{
    public static class CopiesHelper
    {


        public static IEnumerable<DocumentToPrint> GetNonPrintedDocumentCopies(string documentPath, string library, string asunto, string id, string noRadicado)
        {
            SPFolder currentFolder = SPContext.Current.Web.GetFolder(documentPath);
            if (currentFolder.ItemCount == 0)
                return null;
            if (currentFolder == null)
                throw new Exception(string.Format(ExceptionConstants.ERROR_ITEM_NO_ENCONTRADO, currentFolder));
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
                                <And>
                                    <BeginsWith>
                                        <FieldRef Name='ContentTypeId' />
                                        <Value Type='Text'>0x0100d02996621de34fc19e75e1026a6a7af4</Value>
                                    </BeginsWith>
                                    <Eq>
                                        <FieldRef Name='CC_Status_Delivered' />
                                        <Value Type='Text'>False</Value>
                                    </Eq>
                                </And>
                            </Where>";
            query.Folder = currentFolder;
            var copies = SPContext.Current.Web.Lists[library].GetItems(query);
            return (from copy in copies.OfType<SPListItem>()
                    select new DocumentToPrint()
                    {
                        Asunto = asunto,
                        Created = Convert.ToDateTime(copy[SPBuiltInFieldId.Created]),
                        Devuelto = copy[CustomSiteColumns.FLD_COPY_DEVUELTO] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_DEVUELTO]),
                        Enviado = copy[CustomSiteColumns.FLD_COPY_ENVIADO] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_ENVIADO]),
                        Id = copy.ID,
                        Impreso = copy[CustomSiteColumns.FLD_PRINT_COPY] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_PRINT_COPY]),
                        NoRadicado = noRadicado,
                        RemitenteDestinatario = copy[CustomSiteColumns.FLD_COPY_REPRESENTANTE] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_REPRESENTANTE].ToString(),
                        TipoDocumento = "Copia"
                    });
        }


        public static IEnumerable<CopiaRadicado> GetNonPrintedDocumentCopies(string documentPath, string library)
        {
            SPFolder currentFolder = SPContext.Current.Web.GetFolder(documentPath);
            if (currentFolder.ItemCount == 0)
                return null;
            if (currentFolder == null)
                throw new Exception(string.Format(ExceptionConstants.ERROR_ITEM_NO_ENCONTRADO, currentFolder));
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
                                <And>
                                    <BeginsWith>
                                        <FieldRef Name='ContentTypeId' />
                                        <Value Type='Text'>0x0100d02996621de34fc19e75e1026a6a7af4</Value>
                                    </BeginsWith>
                                    <Eq>
                                        <FieldRef Name='CC_Status_Delivered' />
                                        <Value Type='Text'>False</Value>
                                    </Eq>
                                </And>
                            </Where>";
            query.Folder = currentFolder;
            var copies = SPContext.Current.Web.Lists[library].GetItems(query);
            return (from copy in copies.OfType<SPListItem>()
                    select new CopiaRadicado()
                    {
                        CopiaDevuelta = copy[CustomSiteColumns.FLD_COPY_DEVUELTO] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_DEVUELTO]),
                        CopiaEnviada = copy[CustomSiteColumns.FLD_COPY_ENVIADO] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_ENVIADO]),
                        CopiaEnvioCancelada = copy[CustomSiteColumns.FLD_COPY_STATUS_CANCEL] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_STATUS_CANCEL]),
                        CopiaEnvioRecibida = copy[CustomSiteColumns.FLD_COPY_DELIVERED] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_DELIVERED]),
                        CopiaPesada = copy[CustomSiteColumns.FLD_COPY_PESADO] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_PESADO]),
                        Dirección = copy[CustomSiteColumns.FLD_COPY_CONTACT_ADDRESS] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_CONTACT_ADDRESS].ToString(),
                        Email = copy[CustomSiteColumns.FLD_CONTACT_EMAIL] == null ? string.Empty : copy[CustomSiteColumns.FLD_CONTACT_EMAIL].ToString(),
                        EnvioRechazo = copy[CustomSiteColumns.FLD_COPY_MOTIVO_RECHAZO] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_MOTIVO_RECHAZO].ToString(),
                        Id = copy.ID,
                        ID = copy.ID.ToString(),
                        IDMedioDeEnvío = copy[CustomSiteColumns.FLD_COPY_ENVIO_ID] == null ? new Nullable<double>() : Convert.ToDouble(copy[CustomSiteColumns.FLD_COPY_ENVIO_ID]),
                        MedioDeEnvio = copy[CustomSiteColumns.FLD_COPY_ENVIO_NAME] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_ENVIO_NAME].ToString(),
                        Nombres = copy[CustomSiteColumns.FLD_COPY_NAME] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_NAME].ToString(),
                        ObservacionEnvio = copy[CustomSiteColumns.FLD_COPY_COMMENTS] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_COMMENTS].ToString(),
                        RepresentanteLegal = copy[CustomSiteColumns.FLD_COPY_REPRESENTANTE] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_REPRESENTANTE].ToString(),
                        Teléfono = copy[CustomSiteColumns.FLD_COPY_CONTACT_PHONE] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_CONTACT_PHONE].ToString()
                    });
        }

        /// <summary>
        /// Gets the copies of a radicado
        /// </summary>
        /// <param name="documentPath">Path of the radicado</param>
        /// <param name="library">Library container</param>
        /// <returns>Copies collection</returns>
        public static IEnumerable<CopiaRadicado> GetAllDocumentCopies(string documentPath, string library)
        { 
            SPFolder currentFolder = SPContext.Current.Web.GetFolder(documentPath);
            if(currentFolder.ItemCount == 0)
                return null;
            if(currentFolder == null)
                 throw new Exception(string.Format(ExceptionConstants.ERROR_ITEM_NO_ENCONTRADO, currentFolder));
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
                                <BeginsWith>
                                    <FieldRef Name='ContentTypeId' />
                                    <Value Type='Text'>0x0100d02996621de34fc19e75e1026a6a7af4</Value>
                                </BeginsWith>
                            </Where>";
            query.Folder = currentFolder;
            var copies = SPContext.Current.Web.Lists[library].GetItems(query);
            return (from copy in copies.OfType<SPListItem>()
                    select new CopiaRadicado()
                    {
                        CopiaDevuelta = copy[CustomSiteColumns.FLD_COPY_DEVUELTO] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_DEVUELTO]),
                        CopiaEnviada = copy[CustomSiteColumns.FLD_COPY_ENVIADO] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_ENVIADO]),
                        CopiaEnvioCancelada = copy[CustomSiteColumns.FLD_COPY_STATUS_CANCEL] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_STATUS_CANCEL]),
                        CopiaEnvioRecibida = copy[CustomSiteColumns.FLD_COPY_DELIVERED] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_DELIVERED]),
                        CopiaPesada = copy[CustomSiteColumns.FLD_COPY_PESADO] == null ? new Nullable<bool>() : Convert.ToBoolean(copy[CustomSiteColumns.FLD_COPY_PESADO]),
                        Dirección = copy[CustomSiteColumns.FLD_COPY_CONTACT_ADDRESS] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_CONTACT_ADDRESS].ToString(),
                        Email = copy[CustomSiteColumns.FLD_CONTACT_EMAIL] == null ? string.Empty : copy[CustomSiteColumns.FLD_CONTACT_EMAIL].ToString(),
                        EnvioRechazo = copy[CustomSiteColumns.FLD_COPY_MOTIVO_RECHAZO] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_MOTIVO_RECHAZO].ToString(),
                        Id = copy.ID,
                        ID = copy.ID.ToString(),
                        IDMedioDeEnvío = copy[CustomSiteColumns.FLD_COPY_ENVIO_ID] == null ? new Nullable<double>() : Convert.ToDouble(copy[CustomSiteColumns.FLD_COPY_ENVIO_ID]),
                        MedioDeEnvio = copy[CustomSiteColumns.FLD_COPY_ENVIO_NAME] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_ENVIO_NAME].ToString(),
                        Nombres = copy[CustomSiteColumns.FLD_COPY_NAME] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_NAME].ToString(),
                        ObservacionEnvio = copy[CustomSiteColumns.FLD_COPY_COMMENTS] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_COMMENTS].ToString(),
                        RepresentanteLegal = copy[CustomSiteColumns.FLD_COPY_REPRESENTANTE] == null ? string.Empty : copy[CustomSiteColumns.FLD_COPY_REPRESENTANTE].ToString(),
                        Teléfono = copy[CustomSiteColumns.FLD_COPY_CONTACT_PHONE] == null ? string.Empty :copy[CustomSiteColumns.FLD_COPY_CONTACT_PHONE].ToString()
                    });
        }

        /// <summary>
        /// Get an entity of radicado copy
        /// </summary>
        /// <param name="copyItem">Sharepoint item object</param>
        /// <returns>Copy entity</returns>
        public static CopiaRadicado GetCopyEntity(SPListItem copyItem)
        {
            CopiaRadicado copy = new CopiaRadicado();
            copy.CopiaDevuelta = copyItem[CustomSiteColumns.FLD_COPY_DEVUELTO] == null ? new Nullable<bool>() : Convert.ToBoolean(copyItem[CustomSiteColumns.FLD_COPY_DEVUELTO]);
            copy.CopiaEnviada = copyItem[CustomSiteColumns.FLD_COPY_ENVIADO] == null ? new Nullable<bool>() : Convert.ToBoolean(copyItem[CustomSiteColumns.FLD_COPY_ENVIADO]);
            copy.CopiaEnvioCancelada = copyItem[CustomSiteColumns.FLD_COPY_STATUS_CANCEL] == null ? new Nullable<bool>() : Convert.ToBoolean(copyItem[CustomSiteColumns.FLD_COPY_STATUS_CANCEL]);
            copy.CopiaEnvioRecibida = copyItem[CustomSiteColumns.FLD_COPY_DELIVERED] == null ? new Nullable<bool>() : Convert.ToBoolean(copyItem[CustomSiteColumns.FLD_COPY_DELIVERED]);
            copy.CopiaPesada = copyItem[CustomSiteColumns.FLD_COPY_PESADO] == null ? new Nullable<bool>() : Convert.ToBoolean(copyItem[CustomSiteColumns.FLD_COPY_PESADO]);
            copy.Dirección = copyItem[CustomSiteColumns.FLD_COPY_CONTACT_ADDRESS] == null ? string.Empty : copyItem[CustomSiteColumns.FLD_COPY_CONTACT_ADDRESS].ToString();
            copy.Email = copyItem[CustomSiteColumns.FLD_CONTACT_EMAIL] == null ? string.Empty : copyItem[CustomSiteColumns.FLD_CONTACT_EMAIL].ToString();
            copy.EnvioRechazo = copyItem[CustomSiteColumns.FLD_COPY_MOTIVO_RECHAZO] == null ? string.Empty : copyItem[CustomSiteColumns.FLD_COPY_MOTIVO_RECHAZO].ToString();
            copy.Id = copyItem.ID;
            copy.ID = copyItem.ID.ToString();
            copy.IDMedioDeEnvío = copyItem[CustomSiteColumns.FLD_COPY_ENVIO_ID] == null ? new Nullable<double>() : Convert.ToDouble(copyItem[CustomSiteColumns.FLD_COPY_ENVIO_ID]);
            copy.MedioDeEnvio = copyItem[CustomSiteColumns.FLD_COPY_ENVIO_NAME] == null ? string.Empty : copyItem[CustomSiteColumns.FLD_COPY_ENVIO_NAME].ToString();
            copy.Nombres = copyItem[CustomSiteColumns.FLD_COPY_NAME] == null ? string.Empty : copyItem[CustomSiteColumns.FLD_COPY_NAME].ToString();
            copy.ObservacionEnvio = copyItem[CustomSiteColumns.FLD_COPY_COMMENTS] == null ? string.Empty : copyItem[CustomSiteColumns.FLD_COPY_COMMENTS].ToString();
            copy.RepresentanteLegal = copyItem[CustomSiteColumns.FLD_COPY_REPRESENTANTE] == null ? string.Empty : copyItem[CustomSiteColumns.FLD_COPY_REPRESENTANTE].ToString();
            copy.Teléfono = copyItem[CustomSiteColumns.FLD_COPY_CONTACT_PHONE] == null ? string.Empty :copyItem[CustomSiteColumns.FLD_COPY_CONTACT_PHONE].ToString();
            return copy;
        }

    }
}
